#include "main.h"
#include "system.h"

TIM_HandleTypeDef Tim1Handle;
void AdcTriggerTimeInitial(void)
{

  TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  TIM_MasterConfigTypeDef sMasterConfig = {0};
  
  /*****************************
  	HAL_RCC_GetPCLK1Freq=  48000000
	��ʱ���ж�����=PCLK/(Prescaler+1)(Period+1)
	������50HZ һ������20ms��һ�����ڲɼ�64�Ρ�ÿ0.3125ms�ɼ�һ�� H=3200HZ  ADCCLK=8M  208����������
  *****************************/
  Tim1Handle.Instance = TIM1;
  Tim1Handle.Init.Prescaler = 5000 - 1;
  Tim1Handle.Init.CounterMode = TIM_COUNTERMODE_UP;
  Tim1Handle.Init.Period = 3 - 1;
  Tim1Handle.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  Tim1Handle.Init.RepetitionCounter = 0;
  
  Tim1Handle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
  if (HAL_TIM_Base_Init(&Tim1Handle) != HAL_OK)
  {
    Error_Handler();
  }

  /*****************************************
			ѡ��ʱ��Դ��ʹ���ڲ�ʱ��Դ
	******************************************/
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  if (HAL_TIM_ConfigClockSource(&Tim1Handle, &sClockSourceConfig) != HAL_OK)
  {
    Error_Handler();
  }
  
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&Tim1Handle, &sMasterConfig) != HAL_OK)
  {
    Error_Handler();
  }

  HAL_TIM_Base_Start_IT(&Tim1Handle);


 sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;  
 sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
 HAL_TIMEx_MasterConfigSynchronization(&Tim1Handle, &sMasterConfig);

  
}
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef *htim_base)
{
  if (htim_base->Instance == TIM1)
  {
	
    HAL_NVIC_SetPriority(TIM1_BRK_UP_TRG_COM_IRQn, 1, 0);
    HAL_NVIC_EnableIRQ(TIM1_BRK_UP_TRG_COM_IRQn);
    __HAL_RCC_TIM1_CLK_ENABLE();
  }
}

void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef *htim_base)
{
  if (htim_base->Instance == TIM1)
  {
    
    HAL_NVIC_DisableIRQ(TIM1_BRK_UP_TRG_COM_IRQn);
	__HAL_RCC_TIM1_CLK_DISABLE();
  }
}

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
  if (htim->Instance == TIM1)
  {
    
    
  }
}

/*void TIM6_IRQHandler(void)
{
  HAL_TIM_IRQHandler(&Tim1Handle);
}*/

void TIM1_BRK_UP_TRG_COM_IRQHandler(void)
{

	HAL_TIM_IRQHandler(&Tim1Handle);
}